package com.czhlearn.service.impl;

import com.czhlearn.mapper.ArticleMapper;
import com.czhlearn.pojo.Article;
import com.czhlearn.pojo.PageBean;
import com.czhlearn.service.ArticleService;
import com.czhlearn.utils.ThreadLocalUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @program: big-event
 * @description:
 * @author: czh
 * @create: 2023-12-19 14:18
 **/

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public void add(Article article) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setCreateUser(id);
        articleMapper.add(article);

    }

    @Override
    public PageBean<Article> getArticleList(Integer pageNum, Integer pageSize, String categoryId, String state) {
        //创建PagBean对象
        PageBean<Article> pb = new PageBean<>();
        //开启分页 pageHelper
        PageHelper.startPage(pageNum,pageSize);

        // 调用mapper
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        System.out.println(userId+"---"+categoryId+"---"+state);
        List<Article> as = articleMapper.getArticleList(userId,categoryId,state);
        Page<Article> page = (Page<Article>) as;

//        把数据填充到pageBena对象中
        pb.setTotal(page.getTotal());
        pb.setItems(as);
        return pb;
    }

    @Override
    public void update(Article article) {
        article.setUpdateTime(LocalDateTime.now());

        articleMapper.update(article);
    }

    @Override
    public Article detail(Integer id) {
        return articleMapper.detail(id);
    }

    @Override
    public void delete(Integer id) {
            articleMapper.delete(id);
    }
}
